package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 明细服务接口实现
 */
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository repsitory;
@Autowired
private IStrategyService strategyService;
    @Autowired
    private MongoTemplate template;


    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        repsitory.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        repsitory.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, pageable, query, StrategyCatalog.class, qo);
    }

    @Override
    //攻分类查询
    public List<CatalogVO> queryGroupList() {
        List<CatalogVO> list = new ArrayList();
        //1、查询mongodb数据
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg, Map.class);
        List<Map> datas = result.getMappedResults(); //将结果转化成list<map>   map描述一行数据

          //2、封装数据
        for (Map data : datas) {

            CatalogVO vo = new CatalogVO();

            vo.setDestName(data.get("_id").toString());  //id

            List<Object> names = (List<Object>) data.get("names");  //攻略分类的名称集合

            List<Object> ids = (List<Object>) data.get("ids");  //攻略分类的ids集合

            List<StrategyCatalog> catalogList = new ArrayList<>(); //攻略分类对象

            for(int i = 0;i < names.size(); i++){

                StrategyCatalog sc = new StrategyCatalog();

                String name = names.get(i).toString();

                String id = ids.get(i).toString();

                sc.setId(id);

                sc.setName(name);

                catalogList.add(sc);

            }

            vo.setCatalogList(catalogList);

            list.add(vo);

        }
       return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogsByDestId(String destId) {

        //1、查询的分类集合
        List<StrategyCatalog> catalogs= repsitory.findByDestId(destId);
        //2、查询分类对应的明细集合
        for (StrategyCatalog catalog : catalogs) {
            List<Strategy> strategys=strategyService.findByCatalogId(catalog.getId());
            //3、每个catalog都设置进去
            catalog.setStrategies(strategys);
        }


        return catalogs;
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (StringUtils.hasLength(strategyCatalog.getId())) {
            //update
            this.update(strategyCatalog);
        } else {
            //save
            this.save(strategyCatalog);
        }
    }
}
